//
//  DKFaqSourchController.m
//  DanKU
//
//  Created by 吴斌 on 2023/11/11.
//

#import "DKFaqSourchController.h"
#import "DKFaqSourchCell.h"
#import "DKFaqQueryCell.h"
#import "DKFaqQueryEndCell.h"
#import "DKFaqUnQueryAlertView.h"
#import "DKMainCustomerServiceController.h"
@interface DKFaqSourchController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView * faqSourchTableView;
@property (nonatomic, strong)NSMutableArray * faqTypeMdls;
@end

@implementation DKFaqSourchController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatSubView];
}
- (void)creatSubView{
    self.faqSourchTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.faqSourchTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.faqSourchTableView.backgroundColor = [UIColor clearColor];
    self.faqSourchTableView.delegate = self;
    self.faqSourchTableView.dataSource = self;
    self.faqSourchTableView.bounces = NO;
    self.faqSourchTableView.layer.cornerRadius = DK_IsWidthScale(10);
    self.faqSourchTableView.clipsToBounds = YES;
    [self.view addSubview:self.faqSourchTableView];
    [self.faqSourchTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(15)));
        make.right.equalTo(@(-DK_IsWidthScale(15)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
    }];
}
-(void)setFaqTitleMdl:(DKFaqTitleModel *)faqTitleMdl{
    _faqTitleMdl = faqTitleMdl;
    if(_faqTitleMdl){
        [self requestFaqType:_faqTitleMdl];
    }
}
- (void)requestFaqType:(DKFaqTitleModel *)faqTitleMdl{
    WeakSelf(self)
    if(self.sourchContent.length){
        NSDictionary *parmDic = @{@"keyWord":self.sourchContent};
        [DKPublicHttp apiGetQueryFaqListForAPI:parmDic complete:^(NSArray * _Nonnull faqUserMdls) {
            weakSelf.faqTypeMdls = [NSMutableArray arrayWithArray:faqUserMdls];
            [weakSelf.faqSourchTableView reloadData];
        }];
    }else{
        NSDictionary *parmDic = @{@"typeId":faqTitleMdl.id};
        [DKPublicHttp apiGetFaqTypeListForAPI:parmDic complete:^(NSArray * _Nonnull faqUserMdls) {
            weakSelf.faqTypeMdls = [NSMutableArray arrayWithArray:faqUserMdls];
            [weakSelf.faqSourchTableView reloadData];
        }];
    }
}
#pragma mark -- UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.sourchContent.length){
        return self.faqTypeMdls.count+1;
    }else{
        return self.faqTypeMdls.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WeakSelf(self)
    if(self.sourchContent.length){
        if(self.faqTypeMdls.count < (indexPath.row+1)){
            DKFaqQueryEndCell *faqQueryEndCell = [DKFaqQueryEndCell faqQueryEndCell:tableView];
            __block typeof(faqQueryEndCell) faqQueryEndBlockCell = faqQueryEndCell;
            faqQueryEndCell.UpdataUnresolvedBlock = ^{
                [DKFaqUnQueryAlertView dk_ShowFaqUnqueryAlertView:^{
                    DKMainCustomerServiceController *dk_MainCustomerServiceCtrl = [[DKMainCustomerServiceController alloc] init];
                    [weakSelf.navigationController pushViewController:dk_MainCustomerServiceCtrl animated:YES];
                    if(weakSelf.UpdataDataBlock){
                        weakSelf.UpdataDataBlock();
                    }
                } withCancleBlock:^{
                    [faqQueryEndBlockCell faqSourchUpdata];
                }];
            };
            return faqQueryEndCell;
        }else{
            DKFaqQueryCell *faqSourchCell = [DKFaqQueryCell faqQueryCell:tableView];
            DKFaqTypeModel *faqTypeMdl = self.faqTypeMdls[indexPath.row];
            faqSourchCell.indexRow = indexPath.row;
            faqSourchCell.UpdataTableBlock = ^(NSInteger indexRow) {
                faqTypeMdl.isDown = !faqTypeMdl.isDown;
                [weakSelf.faqTypeMdls replaceObjectAtIndex:indexPath.row withObject:faqTypeMdl];
                [weakSelf.faqSourchTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:0];
            };
            faqSourchCell.faqTypeMdl = faqTypeMdl;
            return faqSourchCell;
        }
    }else{
        DKFaqSourchCell *faqSourchCell = [DKFaqSourchCell faqSourchCell:tableView];
        DKFaqTypeModel *faqTypeMdl = self.faqTypeMdls[indexPath.row];
        faqSourchCell.indexRow = indexPath.row;
        if(self.faqTypeMdls.count == (indexPath.row+1)){
            faqSourchCell.isEnd = YES;
        }else{
            faqSourchCell.isEnd = NO;
        }
        faqSourchCell.UpdataTableBlock = ^(NSInteger indexRow) {
            faqTypeMdl.isDown = !faqTypeMdl.isDown;
            [weakSelf.faqTypeMdls replaceObjectAtIndex:indexPath.row withObject:faqTypeMdl];
    //        [weakSelf.faqSourchTableView reloadData];
            [weakSelf.faqSourchTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:0];
        };
        faqSourchCell.UpdataResolvedBlock = ^(NSInteger indexRow) {
            faqTypeMdl.isResolved = YES;
            [weakSelf.faqTypeMdls replaceObjectAtIndex:indexPath.row withObject:faqTypeMdl];
            [weakSelf.faqSourchTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:0];
        };
        faqSourchCell.UpdataUnresolvedBlock = ^(NSInteger indexRow) {
            faqTypeMdl.isUnresolved = YES;
            [weakSelf.faqTypeMdls replaceObjectAtIndex:indexPath.row withObject:faqTypeMdl];
            [weakSelf.faqSourchTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:0];
            [DKFaqUnQueryAlertView dk_ShowFaqUnqueryAlertView:^{//客服
                DKMainCustomerServiceController *dk_MainCustomerServiceCtrl = [[DKMainCustomerServiceController alloc] init];
                [weakSelf.navigationController pushViewController:dk_MainCustomerServiceCtrl animated:YES];
                faqTypeMdl.isUnresolved = NO;
                if(weakSelf.UpdataDataBlock){
                    weakSelf.UpdataDataBlock();
                }
            } withCancleBlock:^{
                faqTypeMdl.isUnresolved = NO;
                [weakSelf requestFaqType:weakSelf.faqTitleMdl];
            }];
        };
        faqSourchCell.faqTypeMdl = faqTypeMdl;
        return faqSourchCell;
    }
}
#pragma mark - JXPagingViewListViewDelegate
- (UIView *)listView {
    return self.view;
}

@end
